// ==UserScript==
// @name Node Maker
// @description This WME Add-Ons plugin create junction on each intersections of selected segments.
// @namespace wme-addons
// @version         0.1
// @match https://world.waze.com/editor/*
// @match https://www.waze.com/editor/*
// @match https://world.waze.com/map-editor/*
// @match https://www.waze.com/map-editor/*
// @include             https://*.waze.com/editor/*
// @include             https://*.waze.com/map-editor/*
// @include             https://*.waze.com/beta_editor/*
// @include             https://descartesw.waze.com/beta/*
// ==/UserScript==

/*
 *  Copyright (c) 2012 - 2013 Gilles Dupeyrou 
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
 */
//Node maker

if ('undefined' == typeof __NM_PAGE_SCOPE_RUN__) {
  (function page_scope_runner() {
    // If we're _not_ already running in the page, grab the full source
    // of this script.
    var my_src = "(" + page_scope_runner.caller.toString() + ")();";

    // Create a script node holding this script, plus a marker that lets us
    // know we are running in the page scope (not the Greasemonkey sandbox).
    // Note that we are intentionally *not* scope-wrapping here.
    var script = document.createElement('script');
    script.setAttribute("type", "text/javascript");
    script.textContent = "var __NM_PAGE_SCOPE_RUN__ = true;\n" + my_src;

    // Insert the script node into the page, so it will run, and immediately
    // remove it to clean up.  Use setTimeout to force execution "outside" of
    // the user script scope completely.
    setTimeout(function() {
	document.body.appendChild(script);
        document.body.removeChild(script);
      }, 0);
  })();

  // Stop running, because we know Greasemonkey actually runs us in
  // an anonymous wrapper.
  return;
}

var nm_version = 0.1;

function initNM(){
  addons.add_menu_entry(addons.buttons['WME-Addons'].menu,'Node Maker',mkNodes,"Create node on each segment intersection");
}
function mkNodes(){

  var lst = selectionManager.selectedItems.clone();
  if (lst.length==0){
    addons.log("No segments selected");
  }else{
    mkNode(lst)
  }
}

function mkNode(segs){
//e = new Waze.Control.NodeCreator(wazeModel, layer, handler, segments);
  for(var i = 0; i < segs.length; i++) {
    for (var j = i+1; j < segs.length; j++){
      var splits = segs[i].geometry.split(segs[j].geometry,{edge: !0,mutual: !0});
      if (splits != null){
	var a = new Waze.Action.AddJunction([segs[i],segs[j]],splits );
	wazeModel.actionManager.add(a);
	segs.splice(i,1);
	segs.splice((j>i)?j-1:j,1);
	segs.push(a.sourceSplits[0],a.sourceSplits[1],a.targetSplits[0],a.targetSplits[1]);
	mkNode(segs)
      }
    }
  }
}

function nm_wait_addons(){
  if((typeof addons === 'undefined')||(addons.buttons['WME-Addons']=== 'undefined')) {
    window.setTimeout(nm_wait_addons,100); 
  }else{
    initNM();
    addons.log("WME Node Maker plugin V"+nm_version+" loaded");
  }

  
}
nm_wait_addons();